package edu.umd.essic.maskbuilder;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author  Pavel Senin
 */
public class Contour {

    private BufferedImage image;
    private static String CURVE_SEPARATOR = "# -b";
    private static String DATA_SEPARATOR = "\t";
    private File ContourFile;
    private float def_mul;
    private float left_x,  upper_y,  nlon,  nlat,  right_x,  lower_y;

    /** Creates a new instance of contour */
    public Contour(File cf, double Def_mul, float Left_x, float Upper_y, float Nlon, float Nlat) {
        left_x = Left_x;
        upper_y = Upper_y;
        right_x = left_x + Nlon;
        if (right_x > 180) {
            right_x = -360 + right_x;
        }
        lower_y = upper_y - Nlat;
        def_mul = (float) Def_mul;
        ContourFile = cf;
    }

    private Point2D.Float GetPoint(String line) {
        float x = 0f;
        int position1 = line.indexOf(DATA_SEPARATOR, 0);
        //int position2 = line.indexOf(DATA_SEPARATOR,position1+2);
        Float xcoord = Float.valueOf(line.substring(0, position1));
        Float ycoord = Float.valueOf(line.substring(position1 + 1, line.length()));
        //if (Math.abs(xcoord.floatValue())>180)
        x = -left_x * def_mul + (xcoord.floatValue()) * def_mul;
        //else
        //    x = left_x*def_mul - (xcoord.floatValue())*def_mul; 
        if ((left_x >= 0) && (right_x < 0) && (xcoord.floatValue() < 0)) {//case E-W and negative value

            x = left_x * def_mul + (xcoord.floatValue()) * def_mul;
        }
        if (left_x < 0) {
            if (xcoord.floatValue() <= -180) {
                x = left_x * def_mul - xcoord.floatValue() * def_mul;
            }
            if (xcoord.floatValue() >= 180) {
                x = -left_x * def_mul + (xcoord.floatValue() - 360) * def_mul;
            }
        }
        /*        if ((left_x <0)&&(right_x<0)){ // case W-W
        if (xcoord.floatValue()<-180)
        x = left_x*def_mul - xcoord.floatValue()*def_mul;
        if (xcoord.floatValue()>=180)
        x = -left_x*def_mul + (xcoord.floatValue()-360)*def_mul; 
        }*/

        /*x = -left_x*def_mul + (xcoord.floatValue())*def_mul;
        //else
        //    x = left_x*def_mul - (180-xcoord.floatValue())*def_mul;
        if(xcoord.floatValue()<0){x = left_x*def_mul + (xcoord.floatValue())*def_mul;}*/
        float y = upper_y * def_mul - ycoord.floatValue() * def_mul;
        Point2D.Float p = new Point2D.Float(x, y);
        return p;
    }

    public void Draw(BufferedImage bi, float zl) {
        String data_line = "";
        boolean path_flag = false;
        Point2D.Float point;
        GeneralPath polyline;

        try {
            Graphics2D drawing = bi.createGraphics();
            //east    
            BufferedReader in = new BufferedReader(new FileReader(ContourFile));
            data_line = null;
            drawing.setColor(Color.RED);
            polyline = new GeneralPath();

            data_line = in.readLine();
            while (data_line != null) {
                if (data_line.equalsIgnoreCase(CURVE_SEPARATOR)) {
                    drawing.draw(polyline);
                    path_flag = false;
                    polyline.reset();

                } else {
                    point = GetPoint(data_line);
                    if (point != null && !(path_flag)) {
                        path_flag = true;
                        polyline.moveTo((float) (point.getX() * zl), (float) (point.getY() * zl));
                    }
                    if (point != null && path_flag) {
                        polyline.lineTo((float) (point.getX() * zl), (float) (point.getY() * zl));
                    }
                }
                data_line = in.readLine();
            }
        } catch (FileNotFoundException e) {
            System.err.println("CheckedIODemo: " + e);
            System.exit(-1);
        } catch (IOException e) {
            System.err.println("CheckedIODemo: " + e);
            System.exit(-1);
        }
    }

    public void Save() {

        float quality = 1.0f;
        String filename = "c://new_contour.jpg";
        image = new BufferedImage((int) (nlon * def_mul), (int) (nlat * def_mul), BufferedImage.TYPE_INT_RGB);
        Graphics2D drawing = image.createGraphics();
        drawing.setColor(Color.WHITE);
        drawing.fillRect(0, 0, (int) (nlon * def_mul - 1), (int) (nlat * def_mul - 1));
        drawing.dispose();
        Draw(image, 1);
        try {
            ByteArrayOutputStream boutstream = new ByteArrayOutputStream();
            JPEGImageEncoder enc = JPEGCodec.createJPEGEncoder(boutstream);
            JPEGEncodeParam enparam = JPEGCodec.getDefaultJPEGEncodeParam(image);
            enparam.setQuality(quality, true);
            enc.encode(image, enparam);
            FileOutputStream fimage = new FileOutputStream(new File(filename));
            boutstream.writeTo(fimage);
            fimage.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    //def_mul = 4;

    }
}
